package com.orange.controller;


import com.github.pagehelper.PageInfo;
import com.orange.commons.Constant;
import com.orange.commons.Result;
import com.orange.domain.Customer;
import com.orange.domain.User;
import com.orange.dto.CustomerDto;
import com.orange.service.CustomerService;
import com.orange.utils.WebMvcUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author Emp
 * @projectName: crm
 * @package: com.powernode.controller
 * @className: CustomerController
 * @description: TODO
 * @date: 2021/11/30 15:25
 * @version: 1.0
 */
@RestController
@RequestMapping("customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    /**
     * 分页查询,按用户@role权限查询客户
     * @param customerDto
     * @return
     */
    @RequestMapping("list.do")
    public Result list(CustomerDto customerDto){
        User cUser = WebMvcUtil.getCurrentUser();
        if (cUser.getRole().equals(Constant.USER_ROLE_2)){
            customerDto.setUserId(cUser.getId());
        }
        PageInfo<Customer> pageInfo = customerService.queryCustomerByPage(customerDto);
        return new Result(0,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 新增客户
     * @param customer
     * @return
     */
    @RequestMapping("addCustomer.do")
    public Result addCustomer(Customer customer){
        try {
            customer.setUserId(WebMvcUtil.getCurrentUser().getId());
            int i = customerService.insertSelective(customer);
           return i==1 ? Result.USER_ADD_SUCCESS : Result.USER_ADD_ERROR;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.USER_ADD_ERROR;
        }
    }

    @RequestMapping("updateCustomer.do")
    public Result updateCustomer(Customer customer){
        try {
            int i = customerService.updateByPrimaryKeySelective(customer);
            return i==1 ? Result.USER_UPDATE_SUCCESS : Result.USER_UPDATE_ERROR;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.USER_UPDATE_ERROR;
        }
    }

    @RequestMapping("delCustomer.do")
    public Result delCustomer(Customer customer){
        try {
            customer.setDeleted(Constant.USER_DELETED_2);
            int i = customerService.updateByPrimaryKeySelective(customer);
            return i==1 ? Result.USER_DEL_SUCCESS : Result.USER_DEL_ERROR;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.USER_DEL_ERROR;
        }
    }

    /**
     * 转移客户,@role=1权限
     * @param userId
     * @param customerIds
     * @return
     */
    @RequestMapping("changeCustomerUser.do")
    public Result changeCustomerUser(Integer userId, Integer[] customerIds){
        System.out.println(Arrays.toString(customerIds));
        try {
            int i = customerService.changeCustomerUser(userId,customerIds);
            return i>=1 ? Result.USER_OPT_SUCCESS : Result.USER_OPT_ERROR;
        }catch (Exception e){
            return Result.USER_OPT_ERROR;
        }
    }
}
